#include "Signal.h"
#include "Source.h"
#include "Sink.h"
#include "plant_1DSOS_RDM_input_1Dsin.h"

// Command line:>> 1DSOS_test.out JOB_ID REPEAT T W A FREQ
// Example: test_plant2D_SPDE_1Dsin.out 1 100 600 1.0 0.5 5

int main(int argc, char** argv){
  double dt	= 1.0;
  int    job_id = atoi(argv[1]);
  int    repeat = atoi(argv[2]);
  double T	= atof(argv[3]);
  double W	= atof(argv[4]);
  double A	= atof(argv[5]);
  double Freq	= atof(argv[6]);
  int    Lx     = atoi(argv[7]);
  double t_end  = atof(argv[8]);

  block* if_W = new source_constant(W);
  block* if_A = new source_constant(A);
  block* if_Freq = new source_constant(Freq);
  block* if_T = new source_constant(T);

  plant_1DSOS_RDM_input_1Dsin* wafer = new plant_1DSOS_RDM_input_1Dsin(dt,Lx,Lx/2,1.8,1.2,0.0);
  
  char   filename[1024];
  sprintf(filename,"Output_h_%d",job_id);
  block* of_h = new Sink_ToFile(filename,t_end,dt,0);
  sprintf(filename,"Output_r2_%d",job_id);
  block* of_r2 = new Sink_ToFile(filename,t_end,dt,0);
  sprintf(filename,"Output_m2_%d",job_id);
  block* of_m2 = new Sink_ToFile(filename,t_end,dt,0);

  wafer->ConnectInputTo(0,*if_W,0);
  wafer->ConnectInputTo(1,*if_A,0);
  wafer->ConnectInputTo(2,*if_Freq,0);
  wafer->ConnectInputTo(3,*if_T,0);

  of_h->ConnectInputTo(0,*wafer,0);
  of_r2->ConnectInputTo(0,*wafer,1);
  of_m2->ConnectInputTo(0,*wafer,2);

  double sysTime = 0.0;
  for(int repeat_counter=0;repeat_counter<repeat;repeat_counter++){
    cout << "repeat #" << repeat_counter+1 << "\n";
    sysTime = 0.0;
    while(sysTime<=t_end){
      if_T ->update(sysTime);
      if_W ->update(sysTime);
      if_A ->update(sysTime);
      if_Freq->update(sysTime);
      wafer->update(sysTime);
      of_h ->update(sysTime);
      of_r2->update(sysTime);
      of_m2->update(sysTime);

      sysTime += dt;
    }
    if(repeat_counter==1){
      sprintf(filename,"snapshot_%d_repeat_%d.dat",job_id,repeat_counter);
      wafer->getSnapshot(filename);
    }

    if_T->reset();
    if_W->reset();
    if_A->reset();
    if_Freq->reset();
    wafer->reset();
    of_h->reset();
    of_r2->reset();
    of_m2->reset();
  }
  // 
  delete if_T;
  delete if_W;
  delete if_A;
  delete if_Freq;
  delete wafer;
  delete of_h;
  delete of_r2;
  delete of_m2;
}
